<?php

namespace App\Console\Commands;

use App\Models\ActivitySale;
use App\Models\ActivityType;
use App\Models\Marketing;
use App\Models\User;
use App\Models\UserAccount;
use App\Models\UserReward;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class Issue extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'issue.issue';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '新人礼包优惠券补发';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $redis = app('redis.connection');
        $issueList = $redis->rpop('issue');
        if (!$issueList){
            w_log('没有要新人礼包优惠券补发' ,'issue');
            return '没有要新人礼包优惠券补发';
        }
        $issueList = json_decode($issueList, true);
//        $activity_type = ActivityType::Where([['status', '1'],['start_at','<=',date('Y-m-d')],['end_at','>=',date('Y-m-d')]])->find();
        if ($issueList['uid'] && $issueList['activity_type_id']){
            $activity = ActivitySale::Where([['status', '1'], ['activity_type_id', $issueList['activity_type_id']]])->get();
            if (!is_null($activity)) {
                try {
                    DB::beginTransaction();
                    foreach ($activity as $item => $value) {
                        $data[$item]['user_id'] = $issueList['uid'];
                        $data[$item]['activity_sale_id'] = $value->id;
                        $data[$item]['activity_type_id'] = $issueList['activity_type_id'];
                        $data[$item]['card_name'] = $value->activity_name;
                        $data[$item]['type_source'] = 2;
                        $data[$item]['full'] = $value->full??0;
                        $data[$item]['money'] = $value->less??0;
                        if ($value->activity_time !== 0) {
                            $data[$item]['time_end_valid'] = strtotime(date('Y-m-d')) + 3600*24*($value->activity_time-1);
                        } else {
                            $data[$item]['time_end_valid'] = 0;
                        }
                        $data[$item]['time_start_valid'] = strtotime(date('Y-m-d'));
                        $data[$item]['created_at'] = date('Y-m-d h:m:s');
                        $value->amount += 1;
                        $value->save();
                    }
                    UserReward::insert($data);
                    DB::commit();
                    Cache::store('redis')->forget('user:info_' . $data[$item]['user_id']);
                    w_log('新人优惠礼包脚本执行成功：【' . json_encode(['uid'=>$issueList['uid'],'activity_type_id'=>$issueList['activity_type_id']]) . '】' ,'issue');
                    echo  '这次优惠券脚本执行完2';
                    return '这次优惠券脚本执行完2';
                } catch (\Exception $e) {
                    DB::rollBack();
                    $redis = app('redis.connection');
                    w_log('新人优惠礼包脚本执行失败：【' . json_encode(['uid'=>$issueList['uid'],'activity_type_id'=>$issueList['activity_type_id']]) . $e->getMessage() . '】' ,'issue');
                    $redisList = $redis->lpush('issue', json_encode(['uid'=>$issueList['uid'],'activity_type_id'=>$issueList['activity_type_id']]));
                    echo  $e->getMessage();
                    return '这次优惠券脚本执行完1';
                }
            }
        }

        echo  '这次优惠券脚本执行完';
        return '这次优惠券脚本执行完';
    }
}
